package com.koi.repect.aspect;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.*;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author ･ᴗ･
 * @description [RedisService]
 */
@Component
public class RedisService {

    @Autowired
    public RedisTemplate redisTemplate;

    /**
     * @param key   缓存的键值
     * @param value 缓存的值
     * @return 缓存的对象
     * @description [缓存基本的对象，Integer、String、实体类等]
     */
    public <T> ValueOperations<String, T> setCacheObject(String key, T value) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, value);
        return operation;
    }

    /**
     * @param key      缓存的键值
     * @param value    缓存的值
     * @param timeout  时间
     * @param timeUnit 时间颗粒度
     * @return 缓存的对象
     * @description [缓存基本的对象，Integer、String、实体类等]
     */
    public <T> ValueOperations<String, T> setCacheObject(String key, T value, Integer timeout, TimeUnit timeUnit) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, value, timeout, timeUnit);
        return operation;
    }

    /**
     * @param key 缓存键值
     * @return 缓存键值对应的数据
     * @description [获得缓存的基本对象]
     */
    public <T> T getCacheObject(String key) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        return operation.get(key);
    }

    /**
     * @description [删除单个对象]
     */
    public void deleteObject(String key) {
        redisTemplate.delete(key);
    }

    /**
     * @description [删除集合对象]
     */
    public void deleteObject(Collection collection) {
        redisTemplate.delete(collection);
    }

    /**
     * @param key      缓存的键值
     * @param dataList 待缓存的List数据
     * @return 缓存的对象
     * @description [缓存List数据]
     */
    public <T> ListOperations<String, T> setCacheList(String key, List<T> dataList) {
        ListOperations listOperation = redisTemplate.opsForList();
        if (null != dataList) {
            int size = dataList.size();
            for (int i = 0; i < size; i++) {
                listOperation.leftPush(key, dataList.get(i));
            }
        }
        return listOperation;
    }

    /**
     * @param key 缓存的键值
     * @return 缓存键值对应的数据
     * @description [获得缓存的list对象]
     */
    public <T> List<T> getCacheList(String key) {
        List<T> dataList = new ArrayList<>();
        ListOperations<String, T> listOperation = redisTemplate.opsForList();
        Long size = listOperation.size(key);

        for (int i = 0; i < size; i++) {
            dataList.add(listOperation.index(key, i));
        }
        return dataList;
    }

    /**
     * @param key     缓存键值
     * @param dataSet 缓存的数据
     * @return 缓存数据的对象
     * @description [缓存Set]
     */
    public <T> BoundSetOperations<String, T> setCacheSet(String key, Set<T> dataSet) {
        BoundSetOperations<String, T> setOperation = redisTemplate.boundSetOps(key);
        Iterator<T> it = dataSet.iterator();
        while (it.hasNext()) {
            setOperation.add(it.next());
        }
        return setOperation;
    }

    /**
     * @description [获得缓存的set]
     */
    public <T> Set<T> getCacheSet(String key) {
        Set<T> dataSet = new HashSet<>();
        BoundSetOperations<String, T> operation = redisTemplate.boundSetOps(key);
        dataSet = operation.members();
        return dataSet;
    }

    /**
     * @description [缓存Map]
     */
    public <T> HashOperations<String, String, T> setCacheMap(String key, Map<String, T> dataMap) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        if (null != dataMap) {
            for (Map.Entry<String, T> entry : dataMap.entrySet()) {
                hashOperations.put(key, entry.getKey(), entry.getValue());
            }
        }
        return hashOperations;
    }

    /**
     * @description [获得缓存的Map]
     */
    public <T> Map<String, T> getCacheMap(String key) {
        Map<String, T> map = redisTemplate.opsForHash().entries(key);
        return map;
    }

    /**
     * @param pattern 字符串前缀
     * @return 对象列表
     * @description [获得缓存的基本对象列表]
     */
    public Collection<String> keys(String pattern) {
        return redisTemplate.keys(pattern);
    }

    /**
     * @description [此key是否存在]
     */
    public boolean haskey(String key) {
        return redisTemplate.hasKey(key);
    }

    /**
     * @description [获取key的过期时间]
     */
    public Long getExpire(String key) {
        return redisTemplate.getExpire(key);
    }


    public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, (T) value);
        return operation;
    }

    /**
     * @param key      缓存的键值
     * @param value    缓存的值
     * @param timeout  时间
     * @param timeUnit 时间颗粒度
     * @return 缓存的对象
     * @description [缓存list<Map < String, Object>>]
     */
    public <T> ValueOperations<String, T> setBillObject(String key, List<Map<String, Object>> value, Integer timeout, TimeUnit timeUnit) {
        ValueOperations<String, T> operation = redisTemplate.opsForValue();
        operation.set(key, (T) value, timeout, timeUnit);
        return operation;
    }

    /**
     * @description [缓存Map]
     */
    public <T> HashOperations<String, String, T> setCKdBillMap(String key, Map<String, T> dataMap) {
        HashOperations hashOperations = redisTemplate.opsForHash();
        if (null != dataMap) {
            for (Map.Entry<String, T> entry : dataMap.entrySet()) {
                hashOperations.put(key, entry.getKey(), entry.getValue());
            }
        }
        return hashOperations;
    }

}
